package com.fengxing.ams.web.facade;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.fengxing.ams.pub.AMSException;
import com.fengxing.ams.pub.constants.ReltCode;
import com.fengxing.ams.web.facade.intf.IRoleFacade;
import com.fengxing.ams.web.facade.mouble.user.IRoleDAO;
import com.fengxing.ams.web.facade.mouble.user.RoleVO;

@Component
@Transactional
public class RoleFacade extends BaseFacade implements IRoleFacade {
	@Autowired
	private IRoleDAO roleDAO;
	
	@Transactional(readOnly=true)
	@Override
	public List<RoleVO> findAllRole() {
		return (List<RoleVO>) roleDAO.findAll();
	}

	@Transactional(readOnly=false)
	@Override
	public RoleVO saveRole(RoleVO role) {
		return roleDAO.save(role);
	}

	@Transactional(readOnly=false)
	@Override
	public void deleteRoleById(Long roleId) {
		int count = roleDAO.countByRoleId(roleId);
		
		if (count > 0) {
			throw new AMSException(ReltCode.ROLEID_FAILD);
		}
		roleDAO.delete(roleId);
	}

	@Override
	public Page<RoleVO> findAllRole(Pageable pageable) {
		return roleDAO.findAll(pageable);
	}
}
